﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using OnlineLearningSystem.IRepository;
using OnlineLearningSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OnlineLearningSystem.Controllers
{
    /// <summary>
    /// 课程管理
    /// </summary>
    public class CourseController : BaseController
    {
        private readonly IConfiguration _configuration;
        public CourseController(IUserRepository userService, ICourseRepository courseService,
            IBatchRepository batchService, ILearningSituationRepository learningSituationService,
            ITestRepository testService, ITopicRepository topicService, IConfiguration configuration)
            : base(userService, courseService, batchService, learningSituationService, testService, topicService)
        {
            _configuration = configuration;
        }

        /// <summary>
        ///课程添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddCourse(Course course)
        {

            int code = 0;
            string message = "";
            try
            {
                code = 200;
                message = "提交成功！";
                if (_courseService.Get(o => o.courseName == course.courseName) != null)
                {
                    code = 500;
                    message = "课程已存在！";
                }
                else
                {
                    _courseService.Create(course);
                }
            }
            catch
            {
                code = 500;
                message = "提交失败！";
            }
            return new JsonResult(new { code, message });
        }

        /// <summary>
        /// 查询全部课程
        /// </summary>
        /// <param name="course"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAllCourse(string courseName)
        {
            int code = 0;
            string message = "";
            IList<Course> data = new List<Course>();
            try
            {
                code = 200;
                message = "查询成功！";
                if (courseName != null&& courseName!="")
                {
                    data.Add(_courseService.Get(o => o.courseName == courseName));
                }
                else
                {
                    data=_courseService.GetAll();
                }
            }
            catch
            {
                code = 500;
                message = "查询失败！";
            }
            return new JsonResult(new { code, message,data });
        }

        /// <summary>
        ///信息修改
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public JsonResult EditCourse(Course course)
        {
            int code = 0;
            string message = "";
            Course data = new Course();
            try
            {
                Course newcourse = _courseService.Get(b => b.ID == course.ID);
                newcourse.courseName = course.courseName;
                newcourse.courseSynopsis = course.courseSynopsis;
                newcourse.times = course.times;
                _courseService.Update(newcourse);
                data = newcourse;
                code = 200;
                message = "成功！";
            }
            catch
            {
                code = 500;
                message = "失败！";
                return new JsonResult(new { code, message });
            };
            return new JsonResult(new { code, message, data });
        }

        /// <summary>
        /// 课程删除
        /// </summary>
        /// <param name="courseId"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult subjectDelete(int courseId)
        {
            int code = 0;
            string message = "";

            Course course = _courseService.Get(o=>o.ID==courseId);


            if (course == null)
            {
                code = 500;
                message = "课程不存在";
            }
            else
            {
                code = 200;
                message = "删除成功！";
                _courseService.Delete(course);
            }
            return new JsonResult(new { code, message });
        }

    }
}
